/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Model;

import Model.Const;
import java.util.Random;

/**
 * Traffic light class that changes traffic light color and generate a string pattern of traffic light.
 * @author Chiaki Sato
 * @since J2SDK 1.6
 * @version 1.0
 */
public class TrafficLight {

    // private instance Variables
    private Random randomTraficLight;
    private int nTrafficLightStatus;
    private String stRedLightHorizTop;
    private String stRedLightHorizBottom;
    private String stRedLightVertTop;
    private String stRedLightVertBottom;

    /**
    * Constructor of traffic light class that initializes instance variables.
    * @param nLaneVert Number of vertical lanes
    */
    public TrafficLight(int nLaneVert){

        randomTraficLight = new Random();
        nTrafficLightStatus = Const.LIGHT_GREEN;
        stRedLightHorizTop = "";
        stRedLightHorizBottom = "";
        stRedLightVertTop = "";
        stRedLightVertBottom = "";
        createTrafficLightStringPattern(nLaneVert);
    }

    /**
    * Public function to get a value of instance variable.
    * @return Horizontal traffic light status
    */
    public int getfTraficLightHoriz() {

        return nTrafficLightStatus;
    }

    /**
    * Public function to get a value of instance variable.
    * @return Vertical traffic light status
    */
    public int getfTraficLightVert() {

        if(nTrafficLightStatus == Const.LIGHT_GREEN){

            return Const.LIGHT_RED;
        }
        else if(nTrafficLightStatus == Const.LIGHT_RED){

            return Const.LIGHT_GREEN;
        }
        return nTrafficLightStatus;
    }

    /**
    * Public function to change the traffic light color.
    */
    public void changeTrafficLightColor(){

        if(nTrafficLightStatus != Const.LIGHT_NONE){

            nTrafficLightStatus = randomTraficLight.nextInt(2);
        }
    }

    /**
    * Private function to create the traffic light string pattern to display.
    * @param nLaneVert Number of vertical lanes
    */
    private void createTrafficLightStringPattern(int nLaneVert){

        int i;

        for (i=0; i<nLaneVert*2+1; i++){

            // red light on horizonal road
            if (i == 0){
                stRedLightHorizTop += Const.SYMBOL_TRAFFIC_LIGHT_RED;
                stRedLightHorizBottom += Const.SYMBOL_TRAFFIC_LIGHT_RED;
            }
            else{
                stRedLightHorizTop += Const.SYMBOL_TRAFFIC_LIGHT_GREEN;
                stRedLightHorizBottom += Const.SYMBOL_TRAFFIC_LIGHT_GREEN;
            }

            // red light on vertical road
            stRedLightVertTop += Const.SYMBOL_TRAFFIC_LIGHT_RED;
            stRedLightVertBottom += Const.SYMBOL_TRAFFIC_LIGHT_GREEN;
        }
    }

    /**
    * Public function to get a string pattern of traffic light for top intersection positon to display.
    * @return String pattern of traffic light for top intersection positon
    */
    public String getTrafficLightStringPatternTop(){

        if (nTrafficLightStatus == Const.LIGHT_RED){        // red light on the horizonal road ?

            return stRedLightHorizTop;
        }
        else{                                               // red light on the vertical road
            return stRedLightVertTop;
        }
    }

    /**
    * Public function to get a string pattern of traffic light for bottom intersection position to display.
    * @return String pattern of traffic light for bottom intersection position
    */
    public String getTrafficLightStringPatternBottom(){

        if (nTrafficLightStatus == Const.LIGHT_RED){           // red light on the vertical road ?

            return stRedLightHorizBottom;
        }
        else{                                           // red light on the vertical road

            return stRedLightVertBottom;
        }
    }

}
